/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.conexion;

import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;
import edu.uoc.tdp.pac4.cliente.Principal;
import edu.uoc.tdp.pac4.admn.*;
import edu.uoc.tdp.pac4.beans.Usuari;
import edu.uoc.tdp.pac4.comun.GestorConexionInterface;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JInternalFrame;


/**
 *
 * @author Nurele
 */
public class GestionConexionLogin extends JInternalFrame {
    
    
   JDesktopPane loginWindow;
   JLabel lbl_user;
   JLabel lbl_pass;
   JLabel lbl_msg;
   JTextField frm_user;
   JPasswordField frm_password;
   JButton btn_login;
    
   
   private GestorConexionInterface conexion;
    
   public GestionConexionLogin(GestorConexionInterface conn){
        
        conexion = conn;
   //             Locale localLocale = new Locale("es", "ES");
     //   TDSLanguageUtils.setDefaultLanguage("i18n/messages");
        
        loginWindow=new JDesktopPane();
        this.add(loginWindow);
        
        /* Etiquetes */
        
        lbl_user = new JLabel();
        lbl_user.setText(TDSLanguageUtils.getMessage("Login.Username"));
        lbl_user.setForeground(Color.white);
        lbl_user.setBounds(100,150,150,25);
        
        lbl_pass = new JLabel();
        lbl_pass.setText(TDSLanguageUtils.getMessage("Login.Password"));
        lbl_pass.setForeground(Color.white);
        lbl_pass.setBounds(100,190,150,25);
        
        /* Si és error, la posem */
        
        lbl_msg = new JLabel();
        lbl_msg.setForeground(Color.red);
        lbl_msg.setBounds(150,40,300,25);        
        
        
        /* Camps de login i pass */
        
        frm_user = new JTextField();
        frm_user.setBounds(200,150,250,25);
        
        frm_password = new JPasswordField();
        frm_password.setBounds(200,190,250,25);
        
        /* Botó */
        
        btn_login = new JButton();
        btn_login.setText(TDSLanguageUtils.getMessage("Login.Login"));
        btn_login.setBounds(250,250,150,40);
        
        
        
          /* Listener clic al botó de login */
        
        this.btn_login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent paramAnonymousActionEvent)   {
                            String username = frm_user.getText();
                            String password = new String(frm_password.getPassword());
                            
                                    
                            
                            if(username.isEmpty() || username.length()<5) {
                                             Principal.showError(TDSLanguageUtils.getMessage("Login.ShortUsername"));
                                       } else if (password.isEmpty()) {
                                             Principal.showError(TDSLanguageUtils.getMessage("Login.NoPassword"));
                                       } else { /* Enviem la petició */
                                try {
                                    int id = conexion.usuariLogin(username, password); 
                                   if( id > 0) {
                                       /* Carregar l'objecte Usuari */ 
                                       Usuari usuari ;
                                       usuari = conexion.loadUsuari(id); 
                                       Principal.currentUser = usuari;
                                       Principal.setMenu();
                                        CerrarVentana();
                                   } else {
                                        Principal.showError(TDSLanguageUtils.getMessage("Login.Error"));
                                   }
                                    
                                } catch (RemoteException e) {
                                   e.printStackTrace();
                               } catch (SQLException e) {
                                   e.printStackTrace();
                               }
                            }
                            
                        };
        });
        /* Dibuixem per pantalla */
        loginWindow.add(lbl_user);
        loginWindow.add(lbl_pass);
        loginWindow.add(frm_user);
        loginWindow.add(frm_password);
        loginWindow.add(btn_login);
        loginWindow.add(lbl_msg);
    }
    private void CerrarVentana() {
                this.dispose();
            }
}
